package cn.itcast.order.service;

import cn.itcast.order.mapper.OrderMapper;
import cn.itcast.order.pojo.Order;
import com.itcast.feign.clients.UserClient;
import com.itcast.feign.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private UserClient userClient;

    /**
     * 使用feign客户端远程调用服务
     * @param orderId
     * @return
     */
    public Order queryOrderById(Long orderId) {
        // 1.查询订单
        Order order = orderMapper.findById(orderId);
        if (order != null){
            User user = userClient.findUserById(order.getUserId());
            order.setUser(user);
        }
        // 4.返回
        return order;
    }
    /**
     * 使用RestTemplate远程调用服务
     */
//    public Order queryOrderById(Long orderId) {
//        // 1.查询订单
//        Order order = orderMapper.findById(orderId);
//        if (order != null){
////            String url = "http://127.0.0.1/user/"+order.getUserId();
//            String url = "http://userservice/user/"+order.getUserId();
//            User user = restTemplate.getForObject(url, User.class);
//            order.setUser(user);
//        }
//        // 4.返回
//        return order;
//    }
}
